package web.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import web.mapper.GoodsMapper;
import web.pojo.Goods;
import web.pojo.User;

@Service
public class GoodsService {

    @Autowired
    private GoodsMapper mapper;

    public boolean addGood(String name, String price) {
        try {
            System.out.println(name);
            System.out.println(price);
            mapper.addGoods(name, new BigDecimal(price));
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public Map<String, Object> queryGoods(Integer page, Integer size) {
        int limit = size == null || size < 1 ? 10 : size;
        // 处理前端提交的页码数
        page = page == null || page < 1 ? 1 : page;
        // 根据页码数、限制量求偏移量
        int offset = (page - 1) * limit;

        List<Goods> users = mapper.queryGoods(offset, limit);
        // 查询总数
        Integer count = mapper.count();
        Map<String, Object> map = new HashMap<>();
        map.put("data", users);
        map.put("total", count);

        return map;
    }

    public boolean deleteGoods(Integer id) {
        try {
            mapper.deleteGoods(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
